package security;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class RandomGenerator {
	
	/**
	 * Génère une chaine de caractère aléatoire de longueur n.
	 * @param length
	 * @return
	 */
	public static String generateRandomString(int length) {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; 
        StringBuffer pass = new StringBuffer();
        for(int x=0;x<length;x++)   {
           int i = (int)Math.floor(Math.random() * (chars.length() -1));
           pass.append(chars.charAt(i));
        }
        return pass.toString();
	}
	
	/**
	 * Génère un nombre de n byte aléatoire.
	 * @param lenght
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] generateSalt(int lenght) throws NoSuchAlgorithmException {
		// see http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#SecureRandom
		SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
		
		byte[] salt = new byte[lenght];
		random.nextBytes(salt);
		return salt;
	}

	public static int generateInt() throws NoSuchAlgorithmException {
		byte[] b = RandomGenerator.generateSalt(4);
		return b[3] & 0xFF |
	            (b[2] & 0xFF) << 8 |
	            (b[1] & 0xFF) << 16 |
	            (b[0] & 0xFF) << 24;
	}
	

	public static void main(String[] args) {
	}

}
